package com.hoodbrains.insideloc.shopping;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.hoodbrains.insideloc.R;
import com.hoodbrains.insideloc.guidedshopping.ShowMapActivity;
import com.hoodbrains.insideloc.locator.ShopListActivity;
import com.hoodbrains.insideloc.utils.ItemHolder;

public class ShoppingListActivity extends ListActivity 
	implements OnClickListener, OnItemClickListener {
	
	
	private ShoppingList model;
	private ShoppingListAdapter sAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.shop_list);
		
//		model = new ShoppingList(this);
		model = ShoppingList.getInstance();
		sAdapter = new ShoppingListAdapter(this, this, model);
		
		setListAdapter(sAdapter);
		
		getListView().setOnItemClickListener(this);
	}
	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == ADD_PRODUCT_ACTIVITY_CODE && data!=null
				&& !model.getItemList().contains(new ShoppingItem(data.getStringExtra("product"),data.getStringExtra("rayon")))) {
			model.add(data.getStringExtra("product"), 
					data.getStringExtra("rayon"));
		}
	}
	
	public void onClick(View v) {
		
		ItemHolder<ShoppingItem> iH = null;
		switch (v.getId()) {
		case R.id.checkbox_sli :
			model.toggleGrabbed(ItemHolder.<ShoppingItem>getItem(v));
			break;
		}
	}


	private static final int ADD_PRODUCT_ACTIVITY_CODE = 18;
	
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		if (position == 0) {
			Intent intent = new Intent(this, RayonChoiceActivity.class);
			startActivityForResult(intent, ADD_PRODUCT_ACTIVITY_CODE);
		
		} else {
			model.toggleGrabbed(ItemHolder.<ShoppingItem>getItem(v));
		}
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.shoplist_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.view_showmap_menu: {

			Intent i = new Intent(this, ShopListActivity.class);
			startActivity(i);
			return true;
		}

		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
